import 'package:flutter/material.dart';
import 'package:flutter_component_tool/src/widgets/cnm_toast/cnm_toast_status.dart';
import 'package:flutter_component_tool/src/widgets/cnm_toast/cnm_toast_theme.dart';
import 'package:flutter_svg/flutter_svg.dart';

/* 
  toast 小部件
 */
class CnmToastWidget extends StatelessWidget {
  final String? msg;
  final CNMToastStatus status;
  final CNMToastTheme theme;

  const CnmToastWidget({
    Key? key,
    required this.status,
    required this.theme,
    this.msg,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return status == CNMToastStatus.text
        ? CnmToastTextWidget(
            msg: msg ?? '',
            status: status,
            theme: theme,
          )
        : CnmToastImageWidget(
            status: status,
            theme: theme,
            msg: msg,
          );
  }
}

/* 
 *  纯文本toast小部件
 */
class CnmToastTextWidget extends StatelessWidget {
  final String msg;
  final CNMToastStatus status;
  final CNMToastTheme theme;

  const CnmToastTextWidget({
    Key? key,
    required this.msg,
    required this.status,
    required this.theme,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Material(
      color: theme.maskBgColor,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            padding: const EdgeInsets.all(12),
            margin: const EdgeInsets.symmetric(horizontal: 12),
            decoration: BoxDecoration(
              color: theme.toastBgColor,
              borderRadius: BorderRadius.circular(theme.borderRadius),
            ),
            child: Text(
              msg,
              style: TextStyle(
                fontSize: theme.fontSize,
                color: theme.fontColor,
                fontWeight: FontWeight.w600,
              ),
            ),
          ),
        ],
      ),
    );
  }
}

/* 
 * 带有图片的toast小部件
 */
class CnmToastImageWidget extends StatelessWidget {
  final String? msg;
  final CNMToastStatus status;
  final CNMToastTheme theme;

  const CnmToastImageWidget({
    Key? key,
    required this.status,
    required this.theme,
    this.msg,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Material(
      color: theme.maskBgColor,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            padding: const EdgeInsets.all(12),
            margin: const EdgeInsets.symmetric(horizontal: 12),
            decoration: BoxDecoration(
              color: theme.toastBgColor,
              borderRadius: BorderRadius.circular(theme.borderRadius),
            ),
            child: Column(
              children: [
                SvgPicture.asset(
                  status == CNMToastStatus.success ? 'assets/dialog_success.svg' : 'assets/dialog_error.svg',
                  package: 'flutter_component_tool',
                  width: 40,
                  height: 40,
                ),
                if (msg != null)
                  Padding(
                    padding: const EdgeInsets.only(top: 2),
                    child: Text(
                      msg!,
                      style: TextStyle(
                        fontSize: theme.fontSize,
                        color: theme.fontColor,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                  ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
